package com.sr.controller.api;

import com.sr.annotation.ApiAuth;
import com.sr.constant.Constant;
import com.sr.controller.BaseController;
import com.sr.enums.ResponseCode;
import com.sr.ibase.IBaseService;
import com.sr.pojo.WResources;
import com.sr.service.IResourcesService;
import com.sr.util.R;
import com.sr.vo.PageVo;
import com.sr.vo.req.ResourcesReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaochi
 * @date 2022/4/27 18:39
 * @desc ResourcesController
 */
@Api(tags = "资源模块")
@RestController
@RequestMapping(Constant.Api.WEB_PREFIX+"/resources")
@RequiredArgsConstructor(onConstructor = @__)
public class ResourcesControllerApi extends BaseController {

    private final IResourcesService resourcesService;

    /**
     * 条件查询资源
     * @return
     */
    @ApiOperation(value = "查询资源",notes = "查询资源")
    @PostMapping("/list/page")
    public R<PageVo<WResources>> list(@RequestBody ResourcesReqVo reqVo){
        IBaseService.Criteria<WResources> criteria = resourcesService.createCriteria(WResources.class);
        criteria.andEqualTo(WResources::getName,!StringUtils.isEmpty(reqVo.getName()),reqVo.getName())
                .andEqualTo(WResources::getIsDel,1);
        return R.ok(resourcesService.findPage(reqVo.getPageNum(),reqVo.getPageSize(),criteria));
    }

    /**
     * 条件查询资源(需要会员登录)
     * @return
     */
    @ApiOperation(value = "查询我的资源列表",notes = "查询我的资源列表")
    @ApiAuth
    @PostMapping("/list/page/manager")
    public R<PageVo<WResources>> pageList(@RequestBody ResourcesReqVo reqVo){
        IBaseService.Criteria<WResources> criteria = resourcesService.createCriteria(WResources.class);
        criteria.andEqualTo(WResources::getName,!StringUtils.isEmpty(reqVo.getName()),reqVo.getName())
                .andEqualTo(WResources::getMemberId,this.getUserId())
                .andNotEqualTo(WResources::getIsDel,0);
        return R.ok(resourcesService.findPage(reqVo.getPageNum(),reqVo.getPageSize(),criteria));
    }

    /**
     * 新增资源
     * @return
     */
    @ApiOperation(value = "新增资源",notes = "新增资源")
    @ApiAuth
    @PostMapping("/create")
    public R<String> create(@RequestBody @Validated(ResourcesReqVo.Create.class) ResourcesReqVo reqVo){
        String userId = this.getUserId();
        if (resourcesService.hasExistsByName(reqVo,userId)){
            return R.error(ResponseCode.NAME_EXISTS_ERROR.getMessage());
        }
        WResources wResources = reqVo.buildEntity();
        wResources.setMemberId(userId);
        return this.response(resourcesService.insert(wResources));
    }

    /**
     * 更新资源
     * @return
     */
    @ApiOperation(value = "更新资源",notes = "更新资源")
    @ApiAuth
    @PostMapping("/update")
    public R<String> update(@RequestBody @Validated(ResourcesReqVo.Update.class) ResourcesReqVo reqVo){
        if (resourcesService.hasExistsByName(reqVo, this.getUserId())){
            return R.error(ResponseCode.NAME_EXISTS_ERROR.getMessage());
        }
        return this.response(resourcesService.updateById(reqVo.buildEntity()));
    }

    /**
     * 删除资源
     * @return
     */
    @ApiOperation(value = "删除资源",notes = "删除资源")
    @ApiAuth
    @GetMapping("/delete/{id}")
    public R<String> delete(@PathVariable String id){
        return this.response(resourcesService.updateById(WResources.builder().id(id).isDel(0).build()));
    }
}